/*
 * osrythmGUI0.java
 *
 * Created on April 14, 2007, 2:21 PM
 */

package osrythm;

import java.awt.event.*;
import javax.swing.Timer;
import java.applet.*;

import java.net.URL;
import java.applet.AudioClip;
import java.applet.Applet;
import java.net.MalformedURLException;


import java.awt.event.*;
/**
 *
 * @author  Fergus Earnshaw
 */
public class OSRythmGUI0 extends javax.swing.JFrame {
    
    
    /** Creates new form osrythmGUI */
    public OSRythmGUI0() {
        initComponents();
        
       try {
           
           //please note I will leave this file hear for as long as posible...
           codeBase = new URL("http://www.viniq.com/dealertools/");//file:///c:/Documents and Settings/Fergus Earnshaw/My Documents/Personal/SCHOOL/CS490CN/project/osrythm/src/sounds/");
           this.writelnToConsole("Click.wav loaded");
       } catch (MalformedURLException e) {
           this.writelnToConsole("Unable to find audio file");
       }
        
        soundList = new SoundList(codeBase);
        soundList.startLoading("click.wav");
        this.clip = soundList.getClip("click.wav");
        
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jbtnCompAddNoteWhole = new javax.swing.JButton();
        jbtnCompAddNoteHalf = new javax.swing.JButton();
        jbtnCompAddNoteQuarter = new javax.swing.JButton();
        jbtnCompAddNote8th = new javax.swing.JButton();
        jbtnCompAddNote16th = new javax.swing.JButton();
        jbtnCompAddNote32nd = new javax.swing.JButton();
        jbtnCompAddRestWhole = new javax.swing.JButton();
        jbtnCompAddRestHalf = new javax.swing.JButton();
        jbtnCompAddRestQuarter = new javax.swing.JButton();
        jbtnCompAddRest8th = new javax.swing.JButton();
        jbtnCompAddRest16th = new javax.swing.JButton();
        jbtnCompAddRest32nd = new javax.swing.JButton();
        jlblComp = new javax.swing.JLabel();
        jbtnCompDelete = new javax.swing.JButton();
        jbtnCompClear = new javax.swing.JButton();
        jbtnMGStop = new javax.swing.JButton();
        jbtnMGPlay = new javax.swing.JButton();
        jlTime = new javax.swing.JLabel();
        jtfComp1 = new javax.swing.JTextField();
        jtfComp2 = new javax.swing.JTextField();
        jtfCompTime = new javax.swing.JTextField();
        jlTempo = new javax.swing.JLabel();
        jbtnCompTempDec = new javax.swing.JButton();
        jbtnCompTempInc = new javax.swing.JButton();
        jtfCompTempo = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tpConsole = new javax.swing.JTextPane();
        jlPenIcon = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OSRythm");
        setResizable(false);
        jbtnCompAddNoteWhole.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/note_32_32.jpg")));
        jbtnCompAddNoteWhole.setToolTipText("Add Whole Note");
        jbtnCompAddNoteWhole.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddNoteWholeActionPerformed(evt);
            }
        });

        jbtnCompAddNoteHalf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/note_16_32.jpg")));
        jbtnCompAddNoteHalf.setToolTipText("Add Half Note");
        jbtnCompAddNoteHalf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddNoteHalfActionPerformed(evt);
            }
        });

        jbtnCompAddNoteQuarter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/note_08_32.jpg")));
        jbtnCompAddNoteQuarter.setToolTipText("Add Quarter Note");
        jbtnCompAddNoteQuarter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddNoteQuarterActionPerformed(evt);
            }
        });

        jbtnCompAddNote8th.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/note_04_32.jpg")));
        jbtnCompAddNote8th.setToolTipText("Add Eigtht Note");
        jbtnCompAddNote8th.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddNote8thActionPerformed(evt);
            }
        });

        jbtnCompAddNote16th.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/note_02_32.jpg")));
        jbtnCompAddNote16th.setToolTipText("Add Sixteenth Note");
        jbtnCompAddNote16th.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddNote16thActionPerformed(evt);
            }
        });

        jbtnCompAddNote32nd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/note_01_32.jpg")));
        jbtnCompAddNote32nd.setToolTipText("Add Thirty-Second Note");
        jbtnCompAddNote32nd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddNote32ndActionPerformed(evt);
            }
        });

        jbtnCompAddRestWhole.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/rest_32_32.jpg")));
        jbtnCompAddRestWhole.setToolTipText("Add Whole Rest");
        jbtnCompAddRestWhole.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddRestWholeActionPerformed(evt);
            }
        });

        jbtnCompAddRestHalf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/rest_16_32.jpg")));
        jbtnCompAddRestHalf.setToolTipText("Add Half Rest");
        jbtnCompAddRestHalf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddRestHalfActionPerformed(evt);
            }
        });

        jbtnCompAddRestQuarter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/rest_08_32.jpg")));
        jbtnCompAddRestQuarter.setToolTipText("Add Quarter Rest");
        jbtnCompAddRestQuarter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddRestQuarterActionPerformed(evt);
            }
        });

        jbtnCompAddRest8th.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/rest_04_32.jpg")));
        jbtnCompAddRest8th.setToolTipText("Add Eigtht Rest");
        jbtnCompAddRest8th.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddRest8thActionPerformed(evt);
            }
        });

        jbtnCompAddRest16th.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/rest_02_32.jpg")));
        jbtnCompAddRest16th.setToolTipText("Add Sixteenth Rest");
        jbtnCompAddRest16th.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddRest16thActionPerformed(evt);
            }
        });

        jbtnCompAddRest32nd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/rest_01_32.jpg")));
        jbtnCompAddRest32nd.setToolTipText("Add Thirty-Second Rest");
        jbtnCompAddRest32nd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompAddRest32ndActionPerformed(evt);
            }
        });

        jlblComp.setText("Composition:");

        jbtnCompDelete.setText("Delete Note");
        jbtnCompDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompDeleteActionPerformed(evt);
            }
        });

        jbtnCompClear.setText("Clear");
        jbtnCompClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompClearActionPerformed(evt);
            }
        });

        jbtnMGStop.setText("STOP");
        jbtnMGStop.setEnabled(false);
        jbtnMGStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnMGStopActionPerformed(evt);
            }
        });

        jbtnMGPlay.setText("PLAY");
        jbtnMGPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnMGPlayActionPerformed(evt);
            }
        });

        jlTime.setText("TIME:");


        jtfComp1.setEnabled(false);
        jtfComp1.setHorizontalAlignment(javax.swing.JTextField.CENTER); 
        jtfComp2.setEnabled(false);               
        jtfComp2.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jtfCompTime.setEditable(false);
        jtfCompTime.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jlTempo.setText("TEMPO:");

        jbtnCompTempDec.setText("-");
        jbtnCompTempDec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompTempDecActionPerformed(evt);
            }
        });

        jbtnCompTempInc.setText("+");
        jbtnCompTempInc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCompTempIncActionPerformed(evt);
            }
        });

        jtfCompTempo.setEditable(false);
        jtfCompTempo.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jScrollPane1.setViewportView(tpConsole);
        this.tpConsole.setEditable(false);

        jlPenIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/pen_st_0.gif")));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfComp1, javax.swing.GroupLayout.DEFAULT_SIZE, 632, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jbtnCompDelete, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jbtnCompAddRestWhole)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompAddRestHalf)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompAddRestQuarter)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbtnCompAddRest8th)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompAddRest16th)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompAddRest32nd))
                            .addComponent(jbtnCompClear, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jbtnCompTempDec, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfCompTempo, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompTempInc))
                            .addComponent(jbtnMGPlay, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                            .addComponent(jbtnMGStop, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbtnCompAddNoteWhole)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompAddNoteHalf)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompAddNoteQuarter)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompAddNote8th)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompAddNote16th)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbtnCompAddNote32nd))
                            .addComponent(jlblComp))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlTempo)
                            .addComponent(jlTime)
                            .addComponent(jtfCompTime, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)))
                    .addComponent(jtfComp2, javax.swing.GroupLayout.DEFAULT_SIZE, 632, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlPenIcon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlPenIcon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlblComp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfComp1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfComp2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jbtnCompAddNoteWhole)
                                .addComponent(jbtnCompAddNoteHalf)
                                .addComponent(jbtnCompAddNoteQuarter))
                            .addComponent(jbtnCompAddNote8th)
                            .addComponent(jbtnCompAddNote16th)
                            .addComponent(jbtnCompAddNote32nd))
                        .addGap(16, 16, 16))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jlTime)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jtfCompTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlTempo)
                        .addGap(7, 7, 7)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jbtnCompAddRestWhole)
                        .addComponent(jbtnCompAddRestHalf)
                        .addComponent(jbtnCompAddRestQuarter))
                    .addComponent(jbtnCompAddRest8th)
                    .addComponent(jbtnCompAddRest16th)
                    .addComponent(jbtnCompAddRest32nd)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbtnCompTempDec)
                            .addComponent(jbtnCompTempInc)
                            .addComponent(jtfCompTempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnMGPlay)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnCompDelete)
                    .addComponent(jbtnCompClear)
                    .addComponent(jbtnMGStop))
                .addContainerGap())
        );
        pack();
        
        //Non machine generated code.
        
        
        
        this.comp = new Composition();
        
        this.writelnToConsole("Thank you for choosing OSRythm...");
        
        this.taskHalf = new ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timerHalfActionPerformed(evt);
            }  
        };
        
        
        this.taskWhole = new ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timerWholeActionPerformed(evt);
            }  
        };
        

        this.updateJFrame();
        
    }
    
    private void getNextNote()
    {
        iCount++;
        
        if(iCount >= iElCount)
        {
            iCount = 0;
        }
        //so now we have the position of the next note; So now we need to know how many 32nd to count...
        
        int i32 = this.comp.getCount(iCount);
        
        this.timerWhole = new Timer((int)(iCount32*i32), this.taskWhole);
        this.timerHalf = new Timer((int)(iCount32*i32/2), this.taskHalf);
        
        this.timerWhole.start();
        this.timerHalf.start();
        
    }

    private void playSound(String strTmp){
        if ((strTmp == "Tick")||(strTmp == "Tock"))
        {
            //TO DO code to play audio.
            clip.play();         
            this.writelnToConsole("["+strTmp+"]");            
        }
        


        
    }
    private void timerWholeActionPerformed(ActionEvent evt)  {
       String strTmp;
       comp.changeState();
       this.timerWhole.stop();
       strTmp = this.comp.getTick(iCount);
       this.writelnToConsole(strTmp);       
       this.playSound(strTmp);
       getNextNote();
       this.updateJFrame();
        
    }
    
    private void timerHalfActionPerformed(ActionEvent evt)  {        
        comp.changeState();
        timerHalf.stop();         
        //this.writelnToConsole(this.comp.getTick(iCount+1));
        this.playSound(this.comp.getTick(iCount+1));
        this.updateJFrame();
    }
    
    private void jbtnCompTempIncActionPerformed(java.awt.event.ActionEvent evt) {                                                
        this.comp.incTempo();
        this.updateJFrame();
    }                                               

    private void jbtnCompTempDecActionPerformed(java.awt.event.ActionEvent evt) {                                                
        this.comp.decTempo();
        this.updateJFrame();
    }                                               

    private void jbtnCompAddRest32ndActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        if(!this.comp.addRest32nd())
        {
            this.writelnToConsole("Unable to add a 32nd rest... You must first delete some elements");   
        }
        
        this.updateJFrame();
    }                                                   

    private void jbtnCompAddRest16thActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        if(!this.comp.addRest16th())
        {
            this.writelnToConsole("Unable to add a 16th rest... You must first delete some elements");  
        }
        
        this.updateJFrame();
    }                                                   

    private void jbtnCompAddRest8thActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        if(!this.comp.addRest8th())
        {
            this.writelnToConsole("Unable to add an 8th rest... You must first delete some elements...");  
        }

        this.updateJFrame();
    }                                                  

    private void jbtnCompAddRestQuarterActionPerformed(java.awt.event.ActionEvent evt) {                                                       
        if(!this.comp.addRestQuarter())
        {
            this.writelnToConsole("Unable to add a Quarter... You must first delete some elements...");  
        }
        
        this.updateJFrame();
    }                                                      

    private void jbtnCompAddRestHalfActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
        if(!this.comp.addRestHalf())
        {
            this.writelnToConsole("Unable to add a Half rest... You must first delete some elements...");  
        }
        
        this.updateJFrame();
    }                                                   

    private void jbtnCompAddRestWholeActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        if(!this.comp.addRestWhole())
        {
            this.writelnToConsole("Unable to add a Whole rest... You must first delete some elements...");  
        }
        this.updateJFrame();
    }                                                    

    private void jbtnCompAddNote32ndActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        
        if(!this.comp.addNote32nd())
        {
            this.writelnToConsole("Unable to add a 32nd note... You must first delete some elements...");  
        }
        
        this.updateJFrame();
    }                                                   

    private void jbtnCompAddNote16thActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        
        if(!this.comp.addNote16th())
        {
            this.writelnToConsole("Unable to add a 16th note... You must first delete some elements...");  
        }
        
        this.updateJFrame();
    }                                                   

    private void jbtnCompAddNote8thActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
        
        if(!this.comp.addNote8th())
        {
            this.writelnToConsole("Unable to add an 8th note... You must first delete some elements...");  
        }
        
        this.updateJFrame();
    }                                                  

    private void jbtnCompAddNoteQuarterActionPerformed(java.awt.event.ActionEvent evt) {                                                       
        // TODO add your handling code here:
        
        if(!this.comp.addNoteQuarter())
        {
            this.writelnToConsole("Unable to add a Quarter note... You must first delete some elements...");  
        }
            
        this.updateJFrame();
    }                                                      

    private void jbtnCompAddNoteHalfActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
        
        if(!this.comp.addNoteHalf())
        {
            this.writelnToConsole("Unable to add a Half note... You must first delete some elements");  
        }
        
        this.updateJFrame();
    }                                                   

    private void jbtnCompAddNoteWholeActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        // TODO add your handling code here:
        
        if(!this.comp.addNoteWhole())
        {
            this.writelnToConsole("Unable to add a Whole note... You must first delete some elements");  
        }
        
            this.updateJFrame();
    }                                                    

    private void jbtnMGStopActionPerformed(java.awt.event.ActionEvent evt) {
        
        this.writelnToConsole("Metrognome stoped...");
        this.jbtnMGPlay.setEnabled(true);
        this.jbtnMGStop.setEnabled(false);
        
        this.jbtnCompAddNote32nd.setEnabled(true);
        this.jbtnCompAddNote16th.setEnabled(true);
        this.jbtnCompAddNote8th.setEnabled(true);
        this.jbtnCompAddNoteQuarter.setEnabled(true);
        this.jbtnCompAddNoteHalf.setEnabled(true);
        this.jbtnCompAddNoteWhole.setEnabled(true);
        
        this.jbtnCompAddRest32nd.setEnabled(true);
        this.jbtnCompAddRest16th.setEnabled(true);
        this.jbtnCompAddRest8th.setEnabled(true);
        this.jbtnCompAddRestQuarter.setEnabled(true);
        this.jbtnCompAddRestHalf.setEnabled(true);
        this.jbtnCompAddRestWhole.setEnabled(true);
        
        this.jbtnCompTempDec.setEnabled(true);
        this.jbtnCompTempInc.setEnabled(true);
        
        this.jbtnCompDelete.setEnabled(true);
        this.jbtnCompClear.setEnabled(true);
        
        this.timerHalf.stop();
        this.timerWhole.stop();
        this.comp.stopComp();
        this.updateJFrame();
    }   
    
    private void jbtnMGPlayActionPerformed(java.awt.event.ActionEvent evt) {                                           
        
        this.iElCount = this.comp.getElementCount();
        if (this.iElCount > 0)
        {
            
        
            this.iCount32 = (int)(60000/(comp.getTempo()*4));      //get tempo returns th number of 8ths so so there is 8
        
            this.iCount = -1;                                      //preset the counter
            this.getNextNote();
                
            this.writelnToConsole("Metrognome started...");
            this.jbtnMGStop.setEnabled(true);
            this.jbtnMGPlay.setEnabled(false);
        
            this.jbtnCompAddNote32nd.setEnabled(false);
            this.jbtnCompAddNote16th.setEnabled(false);
            this.jbtnCompAddNote8th.setEnabled(false);
            this.jbtnCompAddNoteQuarter.setEnabled(false);
            this.jbtnCompAddNoteHalf.setEnabled(false);
            this.jbtnCompAddNoteWhole.setEnabled(false);
        
            this.jbtnCompAddRest32nd.setEnabled(false);
            this.jbtnCompAddRest16th.setEnabled(false);
            this.jbtnCompAddRest8th.setEnabled(false);
            this.jbtnCompAddRestQuarter.setEnabled(false);
            this.jbtnCompAddRestHalf.setEnabled(false);
            this.jbtnCompAddRestWhole.setEnabled(false);
        
            this.jbtnCompTempDec.setEnabled(false);
            this.jbtnCompTempInc.setEnabled(false);
        
            this.jbtnCompDelete.setEnabled(false);
            this.jbtnCompClear.setEnabled(false);
            
            this.comp.startComp();
            
        }
        
        else
        {
           this.writelnToConsole("Unable to play composition... Try adding some elements...") ;
        }
        
        this.updateJFrame();
    }                                     

    private void jbtnCompDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                               

        if(!this.comp.deleteLastTick())
        {
            this.writelnToConsole("Unable to delete element... Try adding some first...");
        }
        
        this.updateJFrame();
    }     
    
    private void jbtnCompClearActionPerformed(java.awt.event.ActionEvent evt) {                                              

        if(this.comp.clearAllTicks())
        {
            this.writelnToConsole("All elements cleared...");
        }
        this.updateJFrame();
    }                                             
    
    private void writelnToConsole(String strTmp){
        this.tpConsole.setText(this.tpConsole.getText() + strTmp +"\r\n");
    }
    
    private void updateJFrame(){        
        this.jlPenIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource(this.comp.getTickState())));      
        this.jtfComp1.setText(this.comp.getTickStr1());
        this.jtfComp2.setText(this.comp.getTickStr2());
        this.jtfCompTime.setText(this.comp.getTime());
        this.jtfCompTempo.setText(Integer.toString(comp.getTempo()));           
    }
    
    /**
     * @param args the command line arguments
     */
    

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new osrythmGUI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnCompAddNote16th;
    private javax.swing.JButton jbtnCompAddNote32nd;
    private javax.swing.JButton jbtnCompAddNote8th;
    private javax.swing.JButton jbtnCompAddNoteHalf;
    private javax.swing.JButton jbtnCompAddNoteQuarter;
    private javax.swing.JButton jbtnCompAddNoteWhole;
    private javax.swing.JButton jbtnCompAddRest16th;
    private javax.swing.JButton jbtnCompAddRest32nd;
    private javax.swing.JButton jbtnCompAddRest8th;
    private javax.swing.JButton jbtnCompAddRestHalf;
    private javax.swing.JButton jbtnCompAddRestQuarter;
    private javax.swing.JButton jbtnCompAddRestWhole;
    private javax.swing.JButton jbtnCompClear;
    private javax.swing.JButton jbtnCompDelete;
    private javax.swing.JButton jbtnCompTempDec;
    private javax.swing.JButton jbtnCompTempInc;
    private javax.swing.JButton jbtnMGPlay;
    private javax.swing.JButton jbtnMGStop;
    private javax.swing.JTextField jtfCompTempo;
    private javax.swing.JLabel jlPenIcon;
    private javax.swing.JLabel jlTempo;
    private javax.swing.JLabel jlTime;
    private javax.swing.JLabel jlblComp;
    private javax.swing.JTextField jtfComp1;
    private javax.swing.JTextField jtfComp2;
    private javax.swing.JTextField jtfCompTime;
    private javax.swing.JTextPane tpConsole;
    //user defined
    private Composition comp;
    
    private int iCount;
    private int iElCount;
    private int iCount32;
    
    
    private javax.swing.Timer timerWhole;
    private javax.swing.Timer timerHalf;
    
    private ActionListener taskHalf;
    private ActionListener taskWhole;
    private URL codeBase;   
    private SoundList soundList;
    private AudioClip clip;
    


    //Sprivate SoundList soundList = new SoundList(this, getCodeBase());
    
    // End of variables declaration                   
    
}
